package com.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.component.domain.SysRoleMenuEntity;
import com.component.domain.SysUserRoleEntity;
import com.component.mapper.SysRoleMenuMapper;
import com.component.service.SysRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.component.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:49
 * @since JDK 1.8
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {

    @Cacheable(cacheNames = "getById",key = "#id")
    @Override
    public SysRoleMenuEntity getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public IPage<SysRoleMenuEntity> page(Integer pageNum, Integer pageSize,String roleId) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        IPage<SysRoleMenuEntity> page=this.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return page;
    }

    @Override
    public SysRoleMenuEntity getSysRoleMenuEntityRidMid(Serializable rid, Serializable mid) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenuEntity::getMenuId,mid);
        lambdaQueryWrapper.eq(SysRoleMenuEntity::getRoleId,rid);
        SysRoleMenuEntity sysRoleMenuEntity=this.baseMapper.selectOne(lambdaQueryWrapper);
        return sysRoleMenuEntity;
    }

    @Override
    public List<SysRoleMenuEntity> getSysRoleMenuEntityRid(Serializable rid) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenuEntity::getRoleId,rid);
        List<SysRoleMenuEntity> list = this.baseMapper.selectList(lambdaQueryWrapper);
        return list;
    }

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public List<SysRoleMenuEntity> getSysRoleMenuEntityUid(Serializable uid) {
        List<SysUserRoleEntity> list=sysUserRoleService.selectUserRoleEntityByUid(uid);
        if(list==null){
            return Collections.emptyList();
        }
        List<Long> roleIds=list.stream().map(SysUserRoleEntity::getRoleId).collect(Collectors.toList());
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysRoleMenuEntity::getRoleId,roleIds);
        List<SysRoleMenuEntity> sysRoleMenuEntities=this.baseMapper.selectList(lambdaQueryWrapper);
        return sysRoleMenuEntities;
    }
}
